<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="deviceForm" :model="deviceForm" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="设备名" prop="deviceName">
            <el-input v-model="deviceForm.deviceName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="设备编码" prop="deviceCode">
            <el-input v-model="deviceForm.deviceCode" disabled />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">测点及规则信息</h4>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['device:device:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['device:device:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['device:device:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['device:device:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="false" />
      <el-table-column label="测点名称" align="center" prop="pointName" />
      <el-table-column label="解析规则名" align="center" prop="resRuleName" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['device:device:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['device:device:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 添加或修改测点与规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="测点">
          <el-select v-model="form.pointId" filterable placeholder="请选择测点">
            <el-option v-for="item in pointOptions" :key="item.id" :label="item.pointName" :value="item.id"
              :disabled="item.status == 1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规则">
          <el-select v-model="form.resRuleId" filterable placeholder="请选择规则">
            <el-option v-for="item in ruleOptions" :key="item.id" :label="item.ruleName" :value="item.id"
              :disabled="item.status == 1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { listDeviceRuleRef, getDeviceRuleRef,addDeviceRuleRef,updateDeviceRuleRef,delDeviceRuleRef,getPoints,getRules,getDevice } from "@/api/device/pointRule";

export default {
  name: "AuthDataCheck",
  data() {
    return {
      // 遮罩层
      loading: true,
      deviceForm: {

      },
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测点与规则表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      pointOptions: [],
      ruleOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pointId: [
          { required: true, message: "测点不能为空", trigger: "change" }
        ],
        resRuleId: [
          { required: true, message: "规则不能为空", trigger: "change" }
        ],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null,
      },
    };
  },
  created() {
    const deviceId = this.$route.params && this.$route.params.deviceId;
    if (deviceId) {
      this.loadData(deviceId);
    }
  },
  methods: {
    /** 查询测点与规则列表 */
    getList() {
      this.loading = true;
      listDeviceRuleRef(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    loadData(deviceId) {
      this.queryParams.deviceId = deviceId;
      this.loading = true;
      getDevice(deviceId).then((response) => {
        this.deviceForm = response.data;
        this.loading = false;
      });
      this.getList();
      getPoints().then(response => {
        this.pointOptions = response.data;
      })
      getRules().then(response => {
        this.ruleOptions = response.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceId: this.queryParams.deviceId,
        pointId: null,
        resRuleId: null,
        sort:0
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getPoints().then(response => {
        this.pointOptions = response.data;
      })
      getRules().then(response => {
        this.ruleOptions = response.data;
      })
      this.open = true;
      this.title = "添加测点与规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPoints().then(response => {
        this.pointOptions = response.data;
      })
      getRules().then(response => {
        this.ruleOptions = response.data;
      })
      getDeviceRuleRef(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改测点与规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeviceRuleRef(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceRuleRef(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除测点与规则编号为"' + ids + '"的数据项？').then(function () {
        return delDeviceRuleRef(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/pointRuleRef/export', {
        ...this.queryParams
      }, `测点与规则_${new Date().getTime()}.xlsx`)
    },
    /** 关闭按钮 */
    close() {
      const obj = { path: "/device/device" };
      this.$tab.closeOpenPage(obj);
    },
  },
};
</script>